<?php

namespace Controller;

use Handler\UserHandler;
use Handler\ZoneHandler;

class WebController extends BaseController {
	/**
	 * 获取配置
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetConfig($clientId, $message) {
		$robots = UserHandler::mb_GetInstance ()->mb_GetAllRobots ();
		$robotData = array ();
		for($i = 0; $i < count ( $robots ); $i ++) {
			// $tmp = array (
			// 'id' => $robots [$i]->m_Id,
			// 'name' => $robots [$i]->m_Name
			// );
			$robotData [] = $robots [$i]->m_Name;
		}
		$data = array (
				'coinvalue' => ZoneHandler::mb_GetInstance ()->m_CoinValue,
				'robots' => $robotData 
		);
		
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $data ) );
	}
	/**
	 * 保存配置
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_SetConfig($clientId, $message) {
		$coinValue = $message ['coinvalue'];
		
		if($coinValue != ZoneHandler::mb_GetInstance ()->m_CoinValue){
			if(count(UserHandler::mb_GetInstance()->mb_GetAllUsers())>0){
				$this->mb_OutHttpMessage($clientId, ErrCode_Toast, '有在线玩家，请稍后在试');
				return ;
			}
		}
		
		
		ZoneHandler::mb_GetInstance ()->mb_SaveCoinValue ( $coinValue );
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 操作机器人
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_HandleRobotName($clientId, $message) {
		$type = $message ['type'];
		$key = $message ['key'];
		
		if ($type == 'add') {
			UserHandler::mb_GetInstance ()->mb_AddRobot ( $key );
		} else {
			$result = UserHandler::mb_GetInstance ()->mb_RemoveRobot ( $key );
			if (! $result) {
				$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, '该机器人正在游戏中，请稍后再试' );
				return;
			}
		}
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取大区配置
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetZoneConfig($clientId, $message) {
		$zoneId = $message ['zoneid'];
		$zone = ZoneHandler::mb_GetInstance ()->mb_GetZones () [$zoneId - 1];
		$data = array (
				'rooms' => count ( $zone ['rooms'] ),
				'enter' => ( string ) $zone ['minenter'] 
		);
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $data ) );
	}
	/**
	 * 设置大区配置
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_SetZoneConfig($clientId, $message) {
		$zoneId = $message ['zoneid'];
		$count = $message ['count'];
		$enter = $message ['enter'];
		
		ZoneHandler::mb_GetInstance ()->mb_UpdateZoneMinEnter ( $zoneId, $enter );
		
		$zone = ZoneHandler::mb_GetInstance ()->mb_GetZones () [$zoneId - 1];
		if ($count > count ( $zone ['rooms'] )) {
			// 新增
			ZoneHandler::mb_GetInstance ()->mb_AddZoneRooms ( $zoneId, $count );
		} else {
			// 减少
			if (ZoneHandler::mb_GetInstance ()->mb_RemoveZoneRooms ( $zoneId, $count ) == false) {
				$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, '房间有玩家，设置房间数量失败' );
				return;
			}
		}
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取房间配置
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetRoomConfig($clientId, $message) {
		$zoneId = $message ['zoneid'];
		$roomNum = $message ['roomnum'];
		
		$zone = ZoneHandler::mb_GetInstance ()->mb_GetZones () [$zoneId - 1];
		$roomId = $zone ['rooms'] [$roomNum - 1];
		$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $roomId );
		
		if (isset ( $room ))
			$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $room->mb_GetRoomConfig () ) );
		else
			$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, '该房间不存在' );
	}
	/**
	 * 设置房间配置
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_SetRoomConfig($clientId, $message) {
		$zoneId = $message ['zoneid'];
		$roomNum = $message ['roomnum'];
		
		$diff = $message ['difficulty'];
		$chouShui = $message ['choushui'];
		$robotCount = $message ['robotcount'];
		
		$zone = ZoneHandler::mb_GetInstance ()->mb_GetZones () [$zoneId - 1];
		$roomId = $zone ['rooms'] [$roomNum - 1];
		$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $roomId );
		
		$room->mb_SetRoomConfig ( $message );
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 设置控杀信息
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_SetControlInfo($clientId, $message) {
		$zoneId = $message ['zoneid'];
		$roomNum = $message ['roomnum'];
		
		$controlId = $message ['controlid'];
		$controlDifficulty = $message ['controldifficulty'];
		
		$zone = ZoneHandler::mb_GetInstance ()->mb_GetZones () [$zoneId - 1];
		$roomId = $zone ['rooms'] [$roomNum - 1];
		$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $roomId );
		
		$room->mb_SetRoomConfig ( $message );
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取游戏记录
	 *
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetRecord($clientId, $message) {
		$type = $message ['type'];
		if ($type == 'zoneroom') {
			// 大区房记录
			$zoneId = $message ['zoneid'];
			$roomNum = $message ['roomnum'];
			$date_0 = $message ['date0'];
			$date_1 = $message ['date1'];
			
			$userId = $message ['id'];
			$userName = $message ['name'];
			
			$where = "zoneid='$zoneId' AND roomnum='$roomNum' AND date>'$date_0' AND date<'$date_1'";
			
			if ($userId != '') {
				$where .= " AND userid='$userId'";
			} else if ($userName != '') {
				$where .= " AND name='$userName'";
			}
		} else if ($type == 'control') {
			
			$date_0 = $message ['date0'];
			$date_1 = $message ['date1'];
			$userId = $message ['id'];
			$userName = $message ['name'];
			
			$where = "iscontrol='是' AND date>'$date_0' AND date<'$date_1'";
			
			if ($userId == '' && $userName == '') {
				// 查找所有控杀者
			} else if ($userId != '') {
				// 按ID查找
				$where .= " AND userid = '$userId'";
			} else if ($userName != '') {
				// 按name查找
				$where .= " AND name = '$userName'";
			}
		} else if ($type == 'name') {
			$userId = $message ['id'];
			$userName = $message ['name'];
			
			$date_0 = $message ['date0'];
			$date_1 = $message ['date1'];
			
			$where = "date>'$date_0' AND date<'$date_1'";
			if ($userId != '') {
				// 按ID查找
				$where .= " AND userid = '$userId'";
			} else if ($userName != '') {
				// 按name查找
				$where .= " AND name = '$userName'";
			}
		} else if ($type == 'num') {
			$num = $message ['num'];
			$date_0 = $message ['date0'];
			$date_1 = $message ['date1'];
			
			$userId = $message ['id'];
			$userName = $message ['name'];
			
			$where = "roomnum='$num' AND date>'$date_0' AND date<'$date_1'";
			if ($userId != '') {
				// 按ID查找
				$where .= " AND userid = '$userId'";
			} else if ($userName != '') {
				// 按name查找
				$where .= " AND name = '$userName'";
			}
		}
		$data = ZoneHandler::mb_GetInstance ()->mb_QueryGameRecord ( $where );
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $data ) );
	}
	/**
	 * 清零 http://127.0.0.1:1027/?zone=Web&method=mb_ClearData
	 */
	public function mb_ClearData($clientId, $message) {
		if (count ( UserHandler::mb_GetInstance ()->mb_GetAllUsers () ) > 0) {
			$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, '有在线玩家，数据清零失败' );
			return;
		}
		UserHandler::mb_GetInstance ()->mb_InitRobots ();
		
		ZoneHandler::mb_GetInstance ()->mb_DeleteData ();
		ZoneHandler::mb_GetInstance ()->mb_Init ();
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, '操作成功' );
	}
	public function mb_TotalAccount($clientId, $message) {
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( ZoneHandler::mb_GetInstance ()->mb_TotalAccount () ) );
	}
	/**
	 * 获取在线玩家
	 * @param unknown $clientId        	
	 * @param unknown $message        	
	 */
	public function mb_GetOnlinePlayers($clientId, $message) {
		$data = array ();
		$users = UserHandler::mb_GetInstance ()->mb_GetAllUsers ();
		foreach ( $users as $user ) {
			$roomId = $user->m_RoomId;
			if ($roomId == - 1) {
				$roomName = '-';
			} else {
				$room = ZoneHandler::mb_GetInstance ()->mb_GetRoom ( $roomId );
				$roomName = $room->m_RoomName;
			}
			
			$data [] = array (
					'id' => (string)$user->m_Id,
					'name' => $user->m_Name,
					'room' => $roomName,
					'coin' => (string)$user->m_Coin 
			);
		}
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, json_encode ( $data ) );
	}
	/**
	 * 
	 * @param unknown $clientId
	 * @param unknown $message
	 */
	public function mb_PlayerRecharge($clientId, $message){
		$name = $message['name'];
		$coin = $message ['coin'];
		
		if($coin < 0){
			$user = UserHandler::mb_GetInstance()->mb_GetUserForName($name);
			if(isset($user)){
				$this->mb_OutHttpMessage ( $clientId, ErrCode_Toast, '该玩家正在游戏中，请稍后再试' );
				return ;
			}
		} 
		
		$this->mb_OutHttpMessage ( $clientId, ErrCode_Normal, ErrMsg_Normal );
	}
}